package com.gopay.bis.web.controller.ebank;

import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import com.gopay.bis.domain.order.OrderInfo;
import com.gopay.common.Result;
import com.gopay.common.constants.bank.BankConstants;
import com.gopay.common.constants.bank.BisBankBusinessProd;
import com.gopay.common.constants.bank.BisBankInterfaceChannel;
import com.gopay.common.domain.bank.BaseMessage;
import com.gopay.common.domain.bank.hxbc.ebank.pay.req.HXEB2CReqPay;

/**
 * 
 * HxbcB2cFrontRes.java
 * 
 * @author LiLiLi
 */
@Controller
@Scope("prototype")
public class HxbcB2cFrontRes extends BankResBase {

	private static Log logger = LogFactory.getLog("HXBC_LOG");

	@Override
	@RequestMapping(value = "/HxbcB2cFrontRes")
	protected ModelAndView execute(HttpServletRequest request,
			HttpServletResponse response) {
		logger.error("--------------- HxbcB2cFrontRes 接收HXBC B2C支付回应处理开始 ---------------");
		return super.execute(request, response);
	}

	@Override
	protected BaseMessage buildMessage(HttpServletRequest request) {
		String hxesbStr = request.getParameter("hxesb");
		logger.info("buildMessage中的hxesbStr="+hxesbStr);
		HXEB2CReqPay message = new HXEB2CReqPay();
		message.setSignMessage(hxesbStr);
		message.setFront(true);
		// 银行业务产品00个人网银支付
		message.setBankBusProd(BisBankBusinessProd._00);
		// 银行接口通道信息 00: 直连
        message.setBankIntrChann(BisBankInterfaceChannel._00);
		message.setBank(BankConstants.BANK_LOGO_HXBC);
		return message;
	}

	@Override
	protected OrderInfo buildOrderInfo(HttpServletRequest request, Result result) {

		OrderInfo orderInfo = new OrderInfo();
		Map<String, String> param = (Map<String, String>) result
				.getModel("isNotifyCashier");
		/**
		 * 订单号
		 */
		String gopayOrderID = param.get("orderId");
		/**
		 * 订单金额
		 */
		String orderAmt = param.get("amt");
		/**
		 * 支付状态
		 */
		String payStatus = param.get("payStatus");
		if ("00".equals(payStatus)) {
			orderInfo.setOrderId(gopayOrderID);
			orderInfo.setAmt(orderAmt);
			orderInfo.setNotifyNum("1");
			orderInfo.setPayStatus("00");
			orderInfo.setRetCode("00");
			orderInfo.setRetMsg("");
		} else {
			orderInfo.setOrderId(gopayOrderID);
			orderInfo.setAmt(orderAmt);
			orderInfo.setNotifyNum("1");
			orderInfo.setPayStatus("01");
			orderInfo.setRetCode("01");
			orderInfo.setRetMsg("");
		}
		return orderInfo;
	}

	@Override
	protected boolean checkParameters(HttpServletRequest request) {
		if (StringUtils.isEmpty(request.getParameter("hxesb"))) {
			return false;
		}
		return true;
	}

}
